#include "widgetsysform.h"
#include "ui_widgetsysform.h"
#include "../gloab/mymessagebox.h"
#include "QsLog/QsLog.h"
#include <QMessageBox>

QSqlDatabase* WidgetSysForm::m_sqlConnect = new QSqlDatabase(QSqlDatabase::addDatabase("QODBC"));

WidgetSysForm::WidgetSysForm(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::WidgetSysForm)
{
    ui->setupUi(this);
    connect(ui->btn_sqlConnect, &QPushButton::clicked, this, &WidgetSysForm::connectSql);
    connect(ui->btn_sqlClose, &QPushButton::clicked, this, &WidgetSysForm::disConnectSql);

}

WidgetSysForm::~WidgetSysForm()
{
    delete ui;
}

QSqlDatabase *WidgetSysForm::getConnect()
{
    if(m_sqlConnect->open()){

        return m_sqlConnect;
    }
    qDebug()<<"数据库未连接";
    return nullptr;
}

void WidgetSysForm::connectSql()
{

    if(!Connection::getInstance()->isConnect()){
        QString ip = ui->edit_sqlIp->text();
        QString dataBaseName = ui->edit_sqlDatebaseName->text();
        int port = ui->edit_sqlPort->text().toInt();
        QString userName = ui->edit_sqlUserName->text();
        QString password = ui->edit_sqlPassword->text();

        bool isConnect = Connection::getInstance()->connect(ip, port, userName, password, dataBaseName);

        if(isConnect){
            qDebug()<<"数据库连接成功";
            MyMessageBox msgBox(this, MsgBoxType_Info, "数据库连接成功");
            msgBox.exec();

            QLOG_INFO()<<CALLPOINTINFO<<"数据库连接成功";
        }else{
            qDebug()<<"数据库连接失败";
            MyMessageBox msgBox(this, MsgBoxType_Info, "数据库连接失败");
            msgBox.exec();
        }
    }

}

void WidgetSysForm::disConnectSql()
{
    Connection::getInstance()->closeConnect();
}
